package com.ruoyi.test.serv.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.test.dao.PersonDao;
import com.ruoyi.test.domain.Person;
import com.ruoyi.test.serv.IPersonServ;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class PersonServImpl implements IPersonServ {
    @Resource
    private PersonDao dao;

    @Override
    public JSONObject listAllPersonOrderByUsername() {
        try {
            // 从DAO层获取排序后的人员列表
            List<Person> personList = dao.listAllPersonOrderByUsername();

            // 遍历列表并将密码字段设置为""
            personList.forEach(person -> person.setPassword(""));

            // 创建JSONObject来存储结果
            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("total", personList.size());
            result.put("data", personList);

            return result;
        } catch (Exception e) {
            JSONObject result = new JSONObject();
            result.put("success", false);
            result.put("message", e.getMessage());
            return result;
        }
    }

    @Override
    public JSONObject login(Person vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "登录失败，用户名或密码不对！");
        int i = dao.login(vo);
        if (i == 1) {
            o.put("success", true);
            o.put("message", "登录成功，用户名密码全对！");
            o.put("vo", dao.findByUsername(vo.getUsername()));
        }

        //升级：写入日志。。。。
        return o;
    }

    @Override
    public Person findPerson(Person vo) {
        return dao.findPerson(vo);
    }

    @Override
    public JSONObject delete(String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "删除操作失败！");
        int i = dao.delete(id);
        if (i > 0) {
            o.put("success", true);
            o.put("message", "删除操作成功！");
        }
        return o;
    }

    @Override
    public Person getOne(String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return dao.getOne(id);
    }

    @Override
    public JSONObject findUser(String param) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "找不到这个人！");
        Person vo = dao.getOne(param);
        if (vo != null) {
            o.put("success", true);
            o.put("message", "找倒起这个人了！");
            o.put("data", vo);
        }
        return o;
    }

    @Override
    public List<Person> listAll() {
        System.out.println(new Throwable().getStackTrace()[0]);
        //记录日志
        //TODO
        return dao.listAll();
    }

    @Override
    public JSONObject listAllNg() {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "一个也没有");
        List<Person> all = dao.listAll();
        if (all.size() > 0) {
            o.put("success", true);
            o.put("message", "个数：" + all.size());
            o.put("data", all);
            //o.put("all", all);
        }
        return o;
    }

    @Override
    public JSONObject update(Person vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "更新操作失败！");
        int i = dao.update(vo);
        if (i > 0) {
            o.put("success", true);
            o.put("message", "更新操作成功！");
        }
        return o;
    }

    @Override
    public JSONObject add(Person vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        JSONObject o = new JSONObject();
        o.put("success", false);
        o.put("message", "新增操作失败！");
        int i = dao.add(vo);
        if (i > 0) {
            o.put("success", true);
            o.put("message", "新增操作成功！");
        }
        return o;
    }

    @Override
    public Object newOper() {
        //TODO
        return null;
    }
}
